package com.sunrise.visual.frame.common
{
	import mx.controls.Alert;

	public class PieUtil
	{
		
		public var config:Object;
		public var data:Array;
		
		public function PieUtil(data:Array,config:Object)
		{
			this.data = data;
			this.config = config;
		}
		
		public function parseXML():XML {
			var wdbm:String=this.config.wdbm;
			var zbbm:Array=this.config.zbbm;
			var zbmc:Array=this.config.zbmc;
			var xmlStr:String="";
			var xml:XML = getXML();
			var paramsColumns:Array = this.config.paramsColumns || [];
			//xml.charts[0].chart[0].data[0]="";
			var seriesColor:Array = this.config.seriesColor || [];
			for(var i:int=0;i<zbbm.length;i++){
				xmlStr='<series name="' + zbmc[i] + '">'; 
				for(var j : int=0; j < this.data.length; j++){
					xmlStr += '<point name="' + this.data[j][wdbm] + '" y="' + (this.data[j][zbbm[i]] || 0) + '"'+(seriesColor[j] ? (' color="' + seriesColor[j] + '"') : '')+'>';
					xmlStr += '<attributes>';
					for(var k:int = 0; k < paramsColumns.length; k++) {
						xmlStr += '<attribute name="' + paramsColumns[k] + '">' + this.data[j][paramsColumns[k]] + '</attribute>';
					}
					xmlStr += '</attributes></point>'
				}
				xmlStr += '</series>';
				xml.charts[0].chart[0].data[0].appendChild(new XML(xmlStr));
			}
			//Alert.show(xml.toString());
			return xml;
		}
		
		private function getXML():XML{
			var xml:XML = <anychart>
								<margin all="0" right={this.config.marginRight || "0"}/>
								  <charts>
									<chart plot_type={config.chartType||"Pie"}>
									  <data_plot_settings default_series_type="Bar">
										<pie_series style="default" radius = {config.radius||"100"}>
										  <tooltip_settings enabled="True">
											<font family="Microsoft Yahei" bold="false" size="12"/>
											<format>{this.config.tooltipFormat || "{%Name}:{%YValue}{numDecimals:0}"}{this.config.format_ZbUnit||''}</format>
										  </tooltip_settings>
											<label_settings enabled={this.config.labelEnabled || "False"}  mode={this.config.labelMode||'Inside'} color="blue" >
												<font family="Microsoft Yahei" bold="false" size="12" color="white"/>
												<format>{this.config.labelFormat || "{%Name}:{%YPercentOfSeries}{numDecimals:2}%"}</format>
												<position anchor={this.config.position_anchor||"CenterTop"} halign="Center" valign="Center" padding="0"/>
											</label_settings>
											<connector color="white"/>
										</pie_series>
									  </data_plot_settings>
									  <chart_settings>
										<title enabled={this.config.titleEnabled || "false"}>
											<text>{this.config.title || ''}</text>
<font family="Microsoft Yahei" bold="True" size="16" color="white" />
										</title>
										<chart_background enabled="false">
										      <inside_margin left={this.config.leftMargin || "0"} top={this.config.topMargin || "0"} right={this.config.rightMargin || "0"} bottom={this.config.bottomMargin || "0"}/>
										</chart_background>
										<data_plot_background enabled="false"/>
										<legend columns={this.config.legendColumns || '1'} enabled={this.config.legendEnabled || 'True'} ignore_auto_item="true" align="center" {this.config.legendHeight?('height="' + this.config.legendHeight + '"'):''} position={this.config.legendPosition || "right"} rows_padding={this.config.legend_row_padding || "1"} >
										<font family="Microsoft Yahei" bold="False" size="12" color="white">
										</font>
										<format>{this.config.legendFormat || "{%Icon}{%Name}\t{%Value}{numDecimals:0}"}</format>
										<background enabled="false"/>
										<title enabled="false"/>
										<items>
										<item source="points" />
										</items>
										</legend>
										<controls>
											<label position="Fixed" anchor={this.config.controlsLabelPosition || "RightTop"} inside_dataplot={this.config.inside_dataplot || "true"} enabled={this.config.controlsLabelEnabled || "false"}>
												<font family="Microsoft Yahei" bold="False" size="12" color="white" />
												<text>{this.config.controlsLabelText || ""}</text>
											</label>
										</controls>
									  </chart_settings>
									  <data>
									  </data>
									</chart>
								  </charts>
								</anychart>;
			return xml;
		}
	}
}